package com.mmall.controller.portal;


import com.mmall.common.Const;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 */
@Controller
@RequestMapping("/user/session")
@Slf4j
public class UserSpringSessionController {

    @Autowired
    private IUserService iUserService;

    /**
     * 用户登陆
     *
     * @param username
     * @param password
     * @param session
     * @return
     */
    @RequestMapping(value = "login", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse login(String username, String password, HttpSession session,
                                HttpServletResponse response) {

        String path = session.getServletContext().getRealPath("upload");

        log.info("Current path:"+path);
        log.info("Login sessionId:{}" , session.getId());
        ServerResponse<User> retResponse = iUserService.login(username, password);
        if (retResponse.isSuccess()) {
            session.setAttribute(Const.CURRENT_USER, retResponse.getData());
           /* //写入cookie中
            CookieUtil.writeLoginToken(response,session.getId());
            //写入redis中
            RedisPoolUtil.setEx(session.getId(),JsonUtil.objToString(retResponse.getData()),
                    Const.RedisExtime.SESSION_EXTIME);*/

        }
        return retResponse;
    }

    /**推出
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "logout", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse logout(HttpSession session,
                                 HttpServletRequest request,HttpServletResponse response) {
        log.info("Logout sessionId:" + request.getSession().getId());
        session.removeAttribute(Const.CURRENT_USER);
        /*String loginToken = CookieUtil.readLoginToken(request);
        //删除cookie
        CookieUtil.delLoginToken(request, response);
        //删除redis的缓存
        RedisPoolUtil.del(loginToken);*/
        return ServerResponse.retSuccess();
    }


    /**获取用户信息
     * @param request
     * @return
     */
    @RequestMapping(value = "getUserInfo",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse getUserInfo(HttpSession session,HttpServletRequest request) {
        log.info("GetUserInfo sessionId:" + request.getSession().getId());
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        /*String loginToken = CookieUtil.readLoginToken(request);
        if (StringUtils.isEmpty(loginToken)) {
            return ServerResponse.retErrorMsg("用户未登录，无法获取信息");
        }
        String userJsonStr = RedisPoolUtil.get(loginToken);
        User user = JsonUtil.stringToObj(userJsonStr, User.class);*/
        if (user != null) {
            return ServerResponse.retSuccessData(user);
        }
        return ServerResponse.retErrorMsg("用户未登录");
    }

    @RequestMapping(value = "test",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse testException(HttpSession session,HttpServletRequest request) {
        log.info("testException sessionId:{}" , request.getSession().getId());

        int i = 0;
        int s = 333 / i;
        System.out.println(s);

        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user != null) {
            return ServerResponse.retSuccessData(user);
        }
        return ServerResponse.retErrorMsg("用户未登录");
    }
}
